#include "BSP/LED/led.h"

/**
 * @brief       LED???
 * @param       ?
 * @retval      ?
 */
void led_init(void)
{
    LED_INIT_FUN();
    for (uint8_t i = 0; i < LED_NUM; i++)
    {
        led_set_state(i, LED_OFF);
    }
}

/**
 * @brief       ??LED??
 * @param       led: LED??
 * @param       state: LED?? (LED_ON or LED_OFF or LED_TOGGLE)
 * @retval      ?
 */
void led_set_state(uint8_t led, uint8_t state)
{
    if (state != LED_TOGGLE)
        LED_GPIO_PIN_WRITE(led, state);
    else
        LED_GPIO_PIN_TOGGLE(led);
}

/**
 * @brief       ??LED??
 * @param       led: LED??
 * @retval      ?
 */
void led_toggle(uint8_t led)
{
    LED_GPIO_PIN_TOGGLE(led);
}

/**
 * @brief       LED ??????????
 * @param       led        : LED ?? 0 ~ LED_NUM-1
 * @param       tick_ms    : ????????? (ms)
 * @param       duty_ms    : ???? (ms)
 * @param       period_ms  : ?? (ms)
 * @note        ???????tick_ms ???
 */
void led_blink_tick(uint8_t led, uint32_t tick_ms, uint32_t duty_ms, uint32_t period_ms)
{
    static uint32_t acc[LED_NUM] = {0}; /* ?? LED ????? */

    if (led >= LED_NUM)
        return;

    acc[led] += tick_ms; /* ???? */

    if (acc[led] >= period_ms) /* ????????? */
        acc[led] = 0;

    if (acc[led] < duty_ms) /* ?????? */
        led_set_state(led, LED_ON);
    else
        led_set_state(led, LED_OFF);
}
